/**
 * Created with JetBrains WebStorm.
 * User: zhaoyanchao
 * Date: 13-7-8
 * Time: 下午1:12
 * To change this template use File | Settings | File Templates.
 */
var http = require('http'),
    mysql = require('mysql'),
    redis = require('redis'),
    weather = require('./getWeather'),
    config = require('./config').config;

var options = {
    hostname: 'product.weather.com.cn',
    port: 80,
    path: '/alarm/grepalarm.php?areaid=[\\d]{5,7}&type=[0-9]{2}',
    method: 'GET'
};

weather.getWeatherBuffers(options,function(data){
    var alarmurl = JSON.parse(data.substring(14,data.length-1));
    var l = alarmurl.data.length;
    for(var i =0;i<l;i++){
        (function(i){
            var url = alarmurl.data[i][1];
            var option = {
                hostname: 'weather.com.cn',
                port: 80,
                path: '/data/alarm/'+url,
                method: 'GET'
            }
            weather.getWeather(option,function(chunk){
                var alarmInfo = JSON.parse(chunk.substring(14,chunk.length));
                //id, time, alertID, province, cityName, signalType, signalLevel, issueTime, issueContent, relieveTime, text
                var nowtime=Math.round(new Date().getTime()/1000);
                var alertID =alarmInfo.ALERTID,
                    province = alarmInfo.PROVINCE,
                    cityName = alarmInfo.CITY,
                    signalType = alarmInfo.SIGNALTYPE,
                    signalLevel = alarmInfo.SIGNALLEVEL,
                    issueTime = alarmInfo.ISSUETIME,
                    issueContent = alarmInfo.ISSUECONTENT,
                    relieveTime = alarmInfo.RELIEVETIME;
                var sql ="INSERT INTO daokeMap.weatherAlarmInfo VALUES ('"+"',from_unixtime("+nowtime+"),'"+alertID+"','"+province+"','"+cityName+"','"+signalType+"','"+signalLevel+"','"+issueTime+"','"+issueContent+"','"+relieveTime+"','"+chunk+"')";
                var connection = mysql.createConnection(config.daokeMap);
                connection.connect();
                connection.query(sql,function(err,res){
                    if(err) throw err;
                    if(res['affectedRows'] ==1)
                        console.log(cityName+' alarmWeather to Mysql OK!')
                    connection.end();
                })
                weather.getAlarmCityCode(url,function(data){
                     var client = redis.createClient(config.redis_port,config.redis_public_host);
                     if(data.length ==1){
                         client.HMSET(data[0]['cityCode']+':alarmWeather','province',province,'cityName',cityName,'signalType',signalType,'signalLevel',signalLevel,'releaseTime',issueTime,'issueContent',issueContent,'relieveTime',relieveTime,function(err,reply){
                             if(err) throw err;
                             console.log(cityName+' alarmWeather to Redis '+reply);
                             client.quit();
                         })
                     }
                })
            })
        })(i)
    }
})